package com.biyeshengjiuye.controller.enterprise;

import com.alibaba.fastjson.JSON;
import com.biyeshengjiuye.entity.Employment;
import com.biyeshengjiuye.entity.Enterprise;
import com.biyeshengjiuye.service.EmploymentService;
import com.biyeshengjiuye.utils.DataGridViewResult;
import com.biyeshengjiuye.utils.SystemConstant;
import com.biyeshengjiuye.vo.EmplVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/enterprise/empl")
public class EmploymentController {

    @Resource
    private EmploymentService employmentService;
    private Enterprise enterprise;

    /**
     * 查询岗位列表
     *
     * @param emplVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(EmplVo emplVo, HttpSession session, Integer eid, Integer check) {
        //设置分页信息
        PageHelper.startPage(emplVo.getPage(), emplVo.getLimit());
        if (check == 1) {
            emplVo.setState("审核中");
        }
        if (eid == 1) {
            enterprise = (Enterprise) session.getAttribute(SystemConstant.ENTERPRISELOGIN);
            if (enterprise != null) {
                emplVo.setE_id(enterprise.getE_id());
            }
        }
        //调用分页查询的方法
        List<Employment> emplList = employmentService.findEmploymentByPageById(emplVo);
        //创建分页对象
        PageInfo<Employment> pageInfo = new PageInfo<Employment>(emplList);
        //返回数据

        return new DataGridViewResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 添加岗位
     *
     * @param employment
     * @return
     */
    @RequestMapping("/addEmpl")
    public String addEmpl(Employment employment, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用添加部门的方法
        enterprise = (Enterprise) session.getAttribute(SystemConstant.ENTERPRISELOGIN);
        if (enterprise != null) {
            employment.setE_id(enterprise.getE_id());
        } else {
            employment.setE_id("admin");
        }

        if (employmentService.addEmpl(employment) > 0) {
            map.put(SystemConstant.SUCCESS, true);//成功
            map.put(SystemConstant.MESSAGE, "添加成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);//失败
            map.put(SystemConstant.MESSAGE, "添加失败");
        }
        //将map集合以JSON格式返回
        return JSON.toJSONString(map);
    }

    /**
     * 修改岗位
     *
     * @param employment
     * @return
     */
    @RequestMapping("/updateEmpl")
    public String updateEmpl(Employment employment) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用修改部门的方法
        if (employmentService.updateEmpl(employment) > 0) {
            map.put(SystemConstant.SUCCESS, true);//成功
            map.put(SystemConstant.MESSAGE, "修改成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);//失败
            map.put(SystemConstant.MESSAGE, "修改失败");
        }
        //将map集合以JSON格式返回
        return JSON.toJSONString(map);
    }


    /**
     * 删除岗位
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用删除部门的方法
        if (employmentService.deleteById(id) > 0) {
            map.put(SystemConstant.SUCCESS, true);//成功
            map.put(SystemConstant.MESSAGE, "删除成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);//失败
            map.put(SystemConstant.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }


    /**
     * 查询热门招聘列表
     */
    @RequestMapping("/hotJobList")
    public void hotJObList(EmplVo emplVo, String pid, HttpSession session) {
        String s = "'"+pid+"'";
        emplVo.setPid(s);
        //调用分页查询的方法
        List<Employment> emplList = employmentService.findHotJobList(emplVo);

        session.setAttribute("hotJobTitle",pid);
        session.setAttribute("hotJobList", emplList);
    }
}
